package com.culturalCenter.dataCenter.service.impl.training;

import cn.hutool.core.util.ObjectUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.training.TrainingTimeLineResponse;
import com.culturalCenter.dataCenter.customEntity.training.*;
import com.culturalCenter.dataCenter.entity.site.SiteAlerts;
import com.culturalCenter.dataCenter.entity.training.Training;
import com.culturalCenter.dataCenter.entity.training.TrainingInfoCount;
import com.culturalCenter.dataCenter.mapper.site.SiteAlertsMapper;
import com.culturalCenter.dataCenter.mapper.training.*;
import com.culturalCenter.dataCenter.service.training.TrainingTimeLineService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : weicai
 * @date : 14:23 2020/10/9
 */
@Service
public class TrainingTimeLineServiceImpl implements TrainingTimeLineService {

    @Resource
    private TrainingMapper trainingMapper;
    @Resource
    private TrainingAuditRecordMapper trainingAuditRecordMapper;
    @Resource
    private TrainingSignUpCountMapper trainingSignUpCountMapper;
    @Resource
    private TrainingLotteryRecordCountMapper trainingLotteryRecordCountMapper;
    @Resource
    private TrainingPeriodsMapper trainingPeriodsMapper;
    @Resource
    private SiteAlertsMapper siteAlertsMapper;
    @Resource
    private TrainingApplyMapper trainingApplyMapper;
    @Resource
    private TrainingWinningMapper trainingWinningMapper;
    @Resource
    private TrainingSignInMapper trainingSignInMapper;
    @Resource
    private TrainingAttendanceMapper trainingAttendanceMapper;
    @Resource
    private TrainingInfoCountMapper trainingInfoCountMapper;

    @Override
    public TrainingTimeLineResponse getTimeLine(String trainingId) {
        TrainingTimeLineResponse trainingTimeLineResponse = new TrainingTimeLineResponse();
        // 时间轴
        Training training = trainingMapper.findTimeLine(trainingId);
        if (ObjectUtil.isNotEmpty(training)) {
            trainingTimeLineResponse.setCreateTime(training.getCreateTime());
            trainingTimeLineResponse.setSignUpBeginTime(training.getSignUpBeginTime());
            trainingTimeLineResponse.setSignUpEndTime(training.getSignUpEndTime());
            trainingTimeLineResponse.setTrainingBeginTime(training.getTrainingBeginTime());
            trainingTimeLineResponse.setTrainingEndTime(training.getTrainingEndTime());
            trainingTimeLineResponse.setBranchName(training.getBranchName());
            trainingTimeLineResponse.setCreateBy(training.getCreateBy());

        }
        // 创建人信息
//        List<Training> trainingList = trainingMapper.findCreatorInfo(trainingId);
//        if (CollectionUtils.isNotEmpty(trainingList)) {
//            trainingTimeLineResponse.setTrainingCreatorList(trainingList);
//        }
        // 报名开始/结束列表
//        List<TrainingInfoCount> signUpEndList = trainingInfoCountMapper.queryByTrainingId(trainingId);
        List<TrainingInfoCount> signUpEndList = trainingInfoCountMapper.queryByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(signUpEndList)) {
            trainingTimeLineResponse.setSignUpBeginList(signUpEndList);
        }
        // 抽签列表
        List<TrainingInfoCount> trainingLotteryRecordCountList = trainingInfoCountMapper.queryLotByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(trainingLotteryRecordCountList)) {
            trainingTimeLineResponse.setLotteryRecordList(trainingLotteryRecordCountList);
        }
        // 签到+考勤列表
        List<CustomTrainingPeriods> customTrainingPeriodsList = trainingPeriodsMapper.queryByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(customTrainingPeriodsList)) {
            trainingTimeLineResponse.setSignInList(customTrainingPeriodsList);
        }
        //讲师
        Training teacher = trainingMapper.queryTeacherByTrainingId(trainingId);
        if (ObjectUtil.isNotEmpty(teacher)) {
            trainingTimeLineResponse.setTeacher(teacher);
        }
        // 现场列表（培训结束）
        List<SiteAlerts> exceptionList = siteAlertsMapper.queryException(trainingId, "", 0, 0);
        if (CollectionUtils.isNotEmpty(exceptionList)) {
            trainingTimeLineResponse.setSiteSituationList(exceptionList);
        }

        return trainingTimeLineResponse;
    }

    @Override
    public List<Training> getApplyTable(String trainingId) {
        return trainingMapper.queryByTrainingId(trainingId);
    }

    @Override
    public List<CustomRegistrationList> getRegistrationListByTrainingId(String trainingId) {
        return trainingApplyMapper.queryByTrainingId(trainingId,null);
    }

    @Override
    public List<CustomRegistrationList> getWinningListByTrainingId(String trainingId) {
        return trainingApplyMapper.queryByTrainingId(trainingId,"admission");
//        return trainingWinningMapper.queryByTrainingId(trainingId);
    }

    @Override
    public List<CustomSignInList> getSignInListByTrainingId(String periodsId) {
        return trainingSignInMapper.queryByTrainingId(periodsId);
    }

    @Override
    public List<CustomAttendanceList> getAttendanceListByTrainingId(String periodsId) {
        return trainingAttendanceMapper.queryByTrainingId(periodsId);
    }

    @Override
    public List<SiteAlerts> getMonitorPicture(String id) {
        return siteAlertsMapper.queryExceptionPicture(id);
    }

}
